<?php

namespace app\api\controller;

use addons\signin\library\Service;
use app\common\controller\Api;

/**
 * 签到接口
 */
class Signin extends Api
{

    protected $noNeedRight = ['*'];

    /**
     * 签到首页
     * @return string
     */
    public function index()
    {
        $config = get_addon_config('signin');
        $signdata = $config['signinscore'];

        list($ranklist, $self_rank, $successions, $is_signin) = Service::getRankInfo();

        $success_day = $successions + 1;
        $score = $signdata['s' . $success_day] ?? $signdata['sn'];

        $msg = $successions ? "你当前已经连续签到 {$successions} 天,明天继续签到可获得 {$score} 积分" : "今天签到可获得 {$score} 积分,请点击签到领取积分";

        $this->success('', [
            'signinscore' => $config['signinscore'], //签到规则
            //'isfillup'    => $config['isfillup'], //是否开启补签
            //'fillupscore' => $config['fillupscore'], //补签消耗积分
            //'fillupdays'  => $config['fillupdays'], //允许补签天数
            //'ranklist'    => $ranklist,
            //'successions' => $successions,
            'successions' => "已连续签到 {$successions} 天",
            'is_signin'   => $is_signin,
            //'self_rank'   => $self_rank, //自己的排名
            'score'       => $this->auth->score, //用户积分
            'msg'         => $msg
        ]);
    }

    //最近七天签到情况
    public function weekSign()
    {
        //$date = $this->request->param('date', date("Y-m"), "trim");
        //$time = strtotime($date);
        $list = \addons\signin\model\Signin::where('user_id', $this->auth->id)
            ->field('id,createtime')
            ->whereTime('createtime', '-7 days')
            //->whereTime('createtime', 'between', [date("Y-m-1", $time), date("Y-m-1", strtotime("+1 month", $time))])
            ->select();
        $newData = [];
        foreach ($list as $index => $item) {
            $newData[date('m.d', $item->createtime)] = date('m.d', $item->createtime);
        }
        $this->success('', $newData);
    }


    /**
     * 每月签到情况
     * @return void
     */
    public function monthSign()
    {
        $date = $this->request->param('date', date("Y-m"), "trim");
        $time = strtotime($date);
        $list = \addons\signin\model\Signin::where('user_id', $this->auth->id)
            ->field('id,createtime')
            ->whereTime('createtime', 'between', [date("Y-m-1", $time), date("Y-m-1", strtotime("+1 month", $time))])
            ->select();
        $newData = [];
        foreach ($list as $index => $item) {
            $newData[date('m.d', $item->createtime)] = date('m.d', $item->createtime);
        }
        $this->success('', $newData);
    }

    /**
     * 立即签到
     */
    public function dosign()
    {
        $config = get_addon_config('signin');
        $signdata = $config['signinscore'];

        $result = Service::dosign($this->auth->id);
        if (isset($result['errmsg'])) {
            $this->error($result['errmsg']);
        } else {
            $this->success('签到成功!连续签到' . $result['successions'] . '天!获得' . $result['score'] . '积分');
        }
    }
}